#!/bin/bash
date_file=`date +%Y%m%d`

#添加
item=UFC-JP_item_CBT
home_pash=/data/www/$item

game_path=/data/cuser00/www/UFC-JP_item_CBT/Game
gameblank_path=/data/cuser00/www/UFC-JP_item_CBT/back_releaseDB/gameb

update_code_path=$home_pash/update_code
md5_path=$update_code_path/md5_home
rsync_path=$update_code_path/rsync
cp_path=$update_code_path/cphome
release_path=$update_code_path/release
releasedate_path=$release_path/$date_file
u_file_list=file_list
work_su_user='su - cuser00 -c'
rsync_game_name=$item
rsync_ip=10.10.20.57
rsync_port=10873

function md5_file
{
#cd $game_path
#find ./ -type f -print0 | xargs -0 md5sum > $update_code_path/mygame.md5
echo ""
echo 更新版本库MD5文件:
cd $gameblank_path
find ./ -type f -print0 | xargs -0 md5sum | sort > $md5_path/mygameb.md5
cd $game_path
find ./ -type f -print0 | xargs -0 md5sum | sort > $md5_path/mygame.md5
}

#rsync同步更新备份目录
function rsync
{
$work_su_user "/usr/bin/rsync --port $rsync_port -avzP  --delete  --password-file=$rsync_path/rcc.pad  gameuser@$rsync_ip::$rsync_game_name  $gameblank_path"

}


#更新游戏目录
function upgame_list
{

md5_c=`cat $md5_path/mygame.md5 | wc -l`
diff $md5_path/mygame.md5 $md5_path/mygameb.md5 | grep ">" > $cp_path/cp_c
diff $md5_path/mygame.md5 $md5_path/mygameb.md5  > $cp_path/cp
cat $cp_path/cp | awk "{print $ 3}" > $cp_path/cpp
cat $cp_path/cp_c | awk "{print $ 3}" > $cp_path/cpp_c
sort $cp_path/cpp |uniq > $cp_path/cppp
sort $cp_path/cpp_c |uniq > $cp_path/cppp_c
#grep -v '^$' $cp_path/cppp > $cp_path/cpppp
cat $cp_path/cppp | grep -v '^$' | grep -v '^-' > $cp_path/cpppp
grep -v '^$' $cp_path/cppp_c > $cp_path/cpppp_c
echo ------ALL DIFF FILE LIST-----
cat $cp_path/cpppp
echo ""
echo ------UPDATE FILE LIST-----
cat $cp_path/cpppp_c
echo ""
echo ------CLEAR FILE LIST-----
rm -rf $release_path/$u_file_list/cl_f
touch $release_path/$u_file_list/cl_f
h=`cat $cp_path/cpppp | wc -l`
hc=`cat $cp_path/cpppp_c | wc -l`
        if [ $md5_c -gt 100 ];then
		diff $cp_path/cpppp $cp_path/cpppp_c | grep '<' >$release_path/$u_file_list/cl_f_one
                cat $release_path/$u_file_list/cl_f_one | cut -b 4- >$release_path/$u_file_list/cl_f
        fi
cat $release_path/$u_file_list/cl_f
}

function upgame_list_no
{

md5_c=`cat $md5_path/mygame.md5 | wc -l`
diff $md5_path/mygame.md5 $md5_path/mygameb.md5 | grep ">" > $cp_path/cp_c
diff $md5_path/mygame.md5 $md5_path/mygameb.md5  > $cp_path/cp
cat $cp_path/cp | awk "{print $ 3}" > $cp_path/cpp
cat $cp_path/cp_c | awk "{print $ 3}" > $cp_path/cpp_c
sort $cp_path/cpp |uniq > $cp_path/cppp
sort $cp_path/cpp_c |uniq > $cp_path/cppp_c
#grep -v '^$' $cp_path/cppp > $cp_path/cpppp
cat $cp_path/cppp | grep -v '^$' | grep -v '^-' > $cp_path/cpppp
grep -v '^$' $cp_path/cppp_c > $cp_path/cpppp_c
#echo ------ALL DIFF FILE LIST-----
#cat $cp_path/cpppp
#echo ""
#echo ------UPDATE FILE LIST-----
#cat $cp_path/cpppp_c
#echo ""
#echo ------CLEAR FILE LIST-----4
rm -rf $release_path/$u_file_list/cl_f
touch $release_path/$u_file_list/cl_f
h=`cat $cp_path/cpppp | wc -l`
hc=`cat $cp_path/cpppp_c | wc -l`
        if [ $md5_c -gt 100 ];then
                diff $cp_path/cpppp $cp_path/cpppp_c | grep '<' >$release_path/$u_file_list/cl_f_one
                cat $release_path/$u_file_list/cl_f_one | cut -b 4- >$release_path/$u_file_list/cl_f
        fi
#cat $release_path/$u_file_list/cl_f
}

function upgame_list_diff
{
echo -n '是否查看::::UPDATE FILE LIST::::文件差异内容(y/n)':
read diff_yn
if [ $diff_yn == "y" ]
    then
    echo ------UPDATE FILE LIST-----
    cat $cp_path/cpppp_c
    echo ---------------------------
    diff_sum=`cat $cp_path/cpppp_c | wc -l`
    #echo diff_sum  $diff_sum
        for ((i=1;i<=$diff_sum;i++))
            do
                diff_path_update=`cat $cp_path/cpppp_c | awk NR==$i |cut -b 2-`
                        diff_dir_path=`dirname $diff_path_update`
                            if [ ! -d $game_path$diff_dir_path ];then
                                    mkdir -p $game_path$diff_dir_path
                            fi
                echo $gameblank_path$diff_path_update "->"  $game_path$diff_path_update
                if [ -f $game_path$diff_path_update ]
                then
                    diff -ups $game_path$diff_path_update $gameblank_path$diff_path_update
                    echo 检查以上文件差异内容:
                    last_continue "差异是否正确?"
                else
                    cat $gameblank_path$diff_path_update
                    echo 检查以上文件差异内容:
                                        last_continue "差异是否正确?"
                fi
            done
    echo ""
fi
}


function upgame_y
{
echo ------UPDATE FILE-----
echo ""
        for ((i=1; i<=$hc; i++))
                do
                path_update=`cat $cp_path/cpppp_c | awk NR==$i |cut -b 2-`
                dir_path=`dirname $path_update`
                if [ ! -d $game_path$dir_path ];then
                        $work_su_user "mkdir -p $game_path$dir_path"
                fi
                echo $gameblank_path$path_update "->"  $game_path$path_update
                $work_su_user "cp -r $gameblank_path$path_update $game_path$path_update"
        done
echo ""
echo ------CLEAR FILE-----
        #cat $release_path/$u_file_list/cl_f
	cl=`cat $release_path/$u_file_list/cl_f | grep -v ^$ | wc -l`
	for ((i=1; i<=$cl; i++))
                do
                path_cl=`cat $release_path/$u_file_list/cl_f | grep -v ^$ | awk NR==$i |cut -b 2-`
                echo clear: $game_path/$path_cl
                rm -rf $game_path/$path_cl

        done
        echo ""
}

function upgame_account
{
md5_file
upgame_list
echo ""
echo 检查以上文件更新列表:
upgame_list_diff
last_continue "是否更新帐号服务器?"
echo ""
upgame_y
}

function upgame
{
md5_file
upgame_list_no
echo ""
echo ""
upgame_y
}


function game_md5_price
{
cd $game_path
find ./ -type f -print0 | xargs -0 md5sum > $md5_path/game_md5_price.md5
cat $md5_path/game_md5_price.md5 | sort > $md5_path/md5_price.md5
echo `md5sum $md5_path/md5_price.md5` | awk '{print $1}'

}

#存放cp cpp cppp cpppp等文件的md5文件提取差异:
function create_update_GAME  
{	
if  [ ! -d  $cp_path ];then
		echo 创建目录：cp_home
		/bin/mkdir -p  $cp_path
		#else
		#echo  "cp_home目录存在"
		
		fi
}

#存放版本.tar文件和md5更新\清除目录文件:
function create_release
{
if [ ! -d $release_path ];then
		echo 创建目录：release
		/bin/mkdir -p $release_path
		#else
		#echo "release 目录存在"
		fi
}

#存放版本的日期文件,定时清楚版本:
function releasedate_file
{
		if [ ! -d $releasedate_path ];then
                        mkdir -p $releasedate_path
                fi
}

function file_list
{
                if [ ! -d $release_path/$u_file_list ];then
                mkdir -p $release_path/$u_file_list
                fi
}

function path_dir
{
		if [ ! -d $game_path ];then
				$work_su_user "mkdir -p $game_path"
		fi
		if [ ! -d $gameblank_path ];then
                $work_su_user "mkdir -p $gameblank_path"
                fi
		if [ ! -d $md5_path ];then
                mkdir -p $md5_path
                fi
		if [ ! -d $rsync_path ];then
                mkdir -p $rsync_path
                fi
		if [ ! -d $cp_path ];then
                mkdir -p $cp_path
                fi
		if [ ! -d $release_path ];then
                mkdir -p $release_path
                fi
}


#等待函数:
function last_continue()
{
    echo -n "$@(y/n [n]):"
    read ret

    if [ $ret != "y" ]
    then
        echo "stop release"
        exit -1
    fi
}



echo ""
#echo GAME目录:$game_path
#rsync
#create_update_GAME

function usage
{
	
	cat << EOU
	OPTIONS:
	-h	print this help
	-y	rsync_gameb
	-z	update accounts
	-u	update code to all server_game
	-m	md5 contrast 
EOU
	exit
}
path_dir
file_list
releasedate_file
create_release
create_update_GAME
while getopts "hyzum" opt; do
	case $opt in
		h) usage;;
		y) rsync;;
		z) upgame_account;;
		u) upgame;;
		m) game_md5_price;;
		*) usage;;
	esac
done







